#include <QMessageBox>
#include "LoginDialog.h"
#include "ui_LoginDialog.h"
#include "Core.h"


LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/trayicon.png"));

    QRect frect = frameGeometry();
    frect.moveCenter(QDesktopWidget().availableGeometry().center());
    move(frect.topLeft());
}

LoginDialog::~LoginDialog()
{
    delete ui;
}
bool LoginDialog::CheckEmail(QString email)
{
    QStringList accountsList = set.AccountsList();
    if(email.endsWith("@gmail.com") && !accountsList.contains(email))
        return true;
    else
        return false;
}

void LoginDialog::on_Create_clicked()
{
    if(CheckEmail(ui->login->text()))
    {
        if(!ui->login->text().isEmpty() && !ui->pass->text().isEmpty())
        {
            set.SetValueForKey(SettingsKeys::KEY_COUNT_ACCOUNT,set.GetValueOfKey(SettingsKeys::KEY_COUNT_ACCOUNT).toInt()+1);
            set.EncryptPassword(ui->login->text(),ui->pass->text());
            set.SetValueForKey(QString("Account/").append(ui->login->text()).append("/notifications"),true);
            emit CreateDatabase(ui->login->text());
            LoginDialog::accept();
        }
        else
        {

            ui->login->setStyleSheet("QLineEdit{border:1px solid red;}");
            ui->pass->setStyleSheet("QLineEdit{border:1px solid red;}");
            ui->labelLogError->setText("Check email or password");
        }
    }
    else
    {
        QMessageBox checkEmailBox;
        checkEmailBox.setIcon(QMessageBox::Warning);
        checkEmailBox.setText("Email exist!!");
        checkEmailBox.exec();
    }

}

void LoginDialog::on_login_textChanged(const QString &arg1)
{
    ui->login->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->pass->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->labelLogError->setText("");
}

void LoginDialog::on_pass_textChanged(const QString &arg1)
{
    ui->login->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->pass->setStyleSheet("QLineEdit{border:1px solid gray;}");
    ui->labelLogError->setText("");
}

